package com.xbongbong.jxc.abstracts;

import com.alibaba.fastjson.JSON;
import com.xbongbong.jxc.help.DataBackFillHelp;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.data.service.RichTextService;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.FormDataHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.dto.FormDataUpdateGetDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.abstracts.AbstractEditPageRenderService;
import com.xbongbong.pro.enums.errorcodes.BuinessDefaultErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/24 15:44
 */
@Component
public abstract class AbstractJxcEditPageRenderService extends AbstractEditPageRenderService<PaasFormEntityExt, PaasFormDataEntityExt> {

    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private DataBackFillHelp dataBackFillHelp;
    @Resource
    private FormDataHelp formDataHelp;
    @Resource
    private RichTextService richTextService;


    @Override
    public PaasFormDataEntityExt getData(FormDataUpdateGetDTO formDataUpdateGetDTO) throws XbbException{
        String corpid = formDataUpdateGetDTO.getCorpid();
        Integer saasMark = formDataUpdateGetDTO.getSaasMark();
        Integer businessType = formDataUpdateGetDTO.getBusinessType();
        Long dataId = formDataUpdateGetDTO.getDataId();
        PaasFormDataEntityExt paasFormDataEntityExt = formDataHelp.getFormDataEntityExt( corpid, saasMark, businessType, dataId);
        richTextService.addRichTextByDataId(dataId, corpid, formDataUpdateGetDTO.getFormId(), businessType, saasMark, paasFormDataEntityExt.getDel(), paasFormDataEntityExt.getData());
        if (paasFormDataEntityExt == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_DATA_NOT_EXIST);
        }
        if (Objects.equals(paasFormDataEntityExt.getDel(), DelEnum.DELETE.getDel())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100404);

        }
        if (FlowStatusEnum.inApproval(paasFormDataEntityExt.getFlowStatus()) && !Objects.equals(businessType, XbbRefTypeEnum.DISTRIBUTOR_ACCOUNT.getCode())) {
            throw new XbbException(BuinessDefaultErrorCodeEnum.API_ERROR_200020);
        }
        // JXC单据拆分旧数据编辑时新增字段回显 com.xbongbong.saas.help.SaasUpdateHelp.formOldData
        dataBackFillHelp.formOldData(paasFormDataEntityExt, businessType);
        return paasFormDataEntityExt;
    }

    @Override
    public PaasFormEntityExt getForm(FormDataUpdateGetDTO formDataUpdateGetDTO) throws XbbException {
        return paasFormModel.getByKey(formDataUpdateGetDTO.getFormId(), formDataUpdateGetDTO.getCorpid());
    }

    @Override
    public List<FieldAttrEntity> getExplain(PaasFormEntityExt paasFormEntityExt, String corpid) throws XbbException {
        PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(paasFormEntityExt.getId(), corpid);
        if ( paasFormExplainEntity == null ) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
        }
        return JSON.parseArray(paasFormExplainEntity.getExplains(), FieldAttrEntity.class);
    }
}
